package com.fintech.third.service.message;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fintech.third.constant.MessageCategoryEnum;
import com.fintech.third.dao.PushMessageInfoMapper;
import com.fintech.third.dao.message.PushMessageCategoryMapper;
import com.fintech.third.model.message.EdcPushMessageCategoryDto;
import com.fintech.third.model.message.EdcUserPushMessageCategoryDto;
import com.fintech.third.model.message.PushMessageInfo;
import com.fintech.third.model.message.UserPushMessageCategoryDto;
import com.marvels.common.utils.MarvelsLogUtil;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

@Service("pushMessageCategoryService")
public class PushMessageCategoryImpl implements PushMessageCategoryService{

	@Autowired
	private PushMessageCategoryMapper pushMessageCategoryMapper;
	@Autowired
	private PushMessageInfoMapper pushMessageInfoMapper;
	@Override
	public boolean updateMessageCategory(EdcUserPushMessageCategoryDto edcUserPushMessageCategoryDto) {
		//先判断用户收到消息对应的大类是否存在
		if(StringUtils.isBlank(edcUserPushMessageCategoryDto.getUserId()) || StringUtils.isBlank(edcUserPushMessageCategoryDto.getCategoryCode())){
			MarvelsLogUtil.getInstance().info("userid or category code is null");
			return false;
		}
		int countUnRead=pushMessageInfoMapper.countPushMessageInfoUnRead(edcUserPushMessageCategoryDto.getUserId(), edcUserPushMessageCategoryDto.getCategoryCode());
		if(countUnRead>0 && edcUserPushMessageCategoryDto.isRead()){
			edcUserPushMessageCategoryDto.setRead(false);
		}
		EdcUserPushMessageCategoryDto edcUserPushMessageCategory=pushMessageCategoryMapper.selectMessageCategory(edcUserPushMessageCategoryDto.getUserId(), edcUserPushMessageCategoryDto.getCategoryCode());
		if(edcUserPushMessageCategory!=null){//数据存在则更新  
			int count=pushMessageCategoryMapper.updateMessageCategory(edcUserPushMessageCategoryDto.getUserId(), edcUserPushMessageCategoryDto.getCategoryCode(), edcUserPushMessageCategoryDto.isRead());
			if(count<1){
				return false;
			}
		}else{//数据不存在则插入
			edcUserPushMessageCategory=new EdcUserPushMessageCategoryDto(); 
			edcUserPushMessageCategory.setCategoryCode(edcUserPushMessageCategoryDto.getCategoryCode());
			edcUserPushMessageCategory.setCreatedBy("sys");
			edcUserPushMessageCategory.setCreatedDate(new Date());
			edcUserPushMessageCategory.setPushAliasId(edcUserPushMessageCategoryDto.getPushAliasId());
			edcUserPushMessageCategory.setRead(edcUserPushMessageCategoryDto.isRead());
			edcUserPushMessageCategory.setPushCategory(edcUserPushMessageCategoryDto.getPushCategory());
			edcUserPushMessageCategory.setUpdatedBy("sys");
			edcUserPushMessageCategory.setUpdatedDate(new Date());
			edcUserPushMessageCategory.setPushCategory(edcUserPushMessageCategoryDto.getPushCategory());
			edcUserPushMessageCategory.setUserId(edcUserPushMessageCategoryDto.getUserId());
			try{
				pushMessageCategoryMapper.insertMessageCategory(edcUserPushMessageCategory);
			}catch(Exception e){
				if(e.getCause() instanceof MySQLIntegrityConstraintViolationException){
					MarvelsLogUtil.getInstance().info("MySQLIntegrityConstraintViolationException exception:"+e.getMessage());
					pushMessageCategoryMapper.updateMessageCategory(edcUserPushMessageCategoryDto.getUserId(), edcUserPushMessageCategoryDto.getCategoryCode(), edcUserPushMessageCategoryDto.isRead());
					return true;
				}else{
					e.printStackTrace();
					MarvelsLogUtil.getInstance().info("insert message category exception:"+e.getMessage());
					return false;
				}
				
			}
		}
		return true;
	}

	@Override
	public List<UserPushMessageCategoryDto> selectUserAllMessageCategory(String userId,String pushCategory) {
		List<UserPushMessageCategoryDto> list=pushMessageCategoryMapper.selectUserAllMessageCategory(userId,pushCategory);
		if(list!=null && list.size()>0){
			for(UserPushMessageCategoryDto userPushMessageCategoryDto:list){
				PushMessageInfo pushMessageInfo=pushMessageInfoMapper.selectPushMessageInfo(userId, userPushMessageCategoryDto.getCategoryCode());
				if(pushMessageInfo==null){//
					MarvelsLogUtil.getInstance().info("select user push message info first not read no data userId:"+userId);
					userPushMessageCategoryDto.setRead(true);//为空则设置成已读
					EdcUserPushMessageCategoryDto edcUserPushMessageCategoryDto=new EdcUserPushMessageCategoryDto();
					edcUserPushMessageCategoryDto.setRead(true);
					edcUserPushMessageCategoryDto.setUserId(userId);
					edcUserPushMessageCategoryDto.setPushCategory(userPushMessageCategoryDto.getPushCategory());
					updateMessageCategory(edcUserPushMessageCategoryDto);
				}else{
					String content="";
					if(userPushMessageCategoryDto.getCategoryCode().equals(MessageCategoryEnum.SystemBulletin) || userPushMessageCategoryDto.getCategoryCode().equals(MessageCategoryEnum.SelectedActivities)){//系统公告  精选活动则取消息标题
						content=pushMessageInfo.getTitle();
					}else{
						content=pushMessageInfo.getContent();
					}
					userPushMessageCategoryDto.setContent(content);
					userPushMessageCategoryDto.setDate(pushMessageInfo.getCreatedDate());
					PushMessageInfo pushMessage=pushMessageInfoMapper.selectPushMessageInfoUnRead(userId, userPushMessageCategoryDto.getCategoryCode());//查询未读消息时间最大的一条
					if(pushMessage==null){//为空则已读
						userPushMessageCategoryDto.setRead(true);//为空则设置成已读
						EdcUserPushMessageCategoryDto edcUserPushMessageCategoryDto=new EdcUserPushMessageCategoryDto();
						edcUserPushMessageCategoryDto.setRead(true);
						edcUserPushMessageCategoryDto.setUserId(userId);
						edcUserPushMessageCategoryDto.setPushCategory(userPushMessageCategoryDto.getPushCategory());
						updateMessageCategory(edcUserPushMessageCategoryDto);
					}
				}
				
			}
		}else{
			MarvelsLogUtil.getInstance().info("select user all message category no data userId:"+userId);
		}
		return list;
	}

	@Override
	public List<EdcPushMessageCategoryDto> selectEdcPushMessageCategory(String pushCategory) {
		return pushMessageCategoryMapper.selectEdcPushMessageCategory(pushCategory);
	}

}
